package com.fitbet.wechat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fitbet.entity.bet.Bet;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.bet.UserBetSupport;
import com.fitbet.entity.bet.UserBetSupportStatus;
import com.fitbet.entity.trading.SalesOrder;
import com.fitbet.entity.user.User;
import com.fitbet.wechat.dao.UserBetDao;
import com.fitbet.wechat.service.UserBetService;
/**
 * 
 * @author sjh
 *
 */
@Service("userBetService")
public class UserBetServiceImpl implements UserBetService{
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dao
    private UserBetDao userBetDao;
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#list(java.lang.Long, com.fitbet.entity.bet.UserBetStatus[])
     */
    public List<UserBet> list(Long userId, UserBetStatus[] status) {
        return userBetDao.list(userId, status);
    }
    

    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#listHotUserBet(com.fitbet.entity.bet.UserBetStatus[])
     */
    public List<UserBet> listHotUserBet(UserBetStatus[] status) {
        return userBetDao.listHot(status, 0 , 30);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#listUserBetSupport(java.lang.Long, com.fitbet.entity.bet.UserBetSupportStatus[])
     */
    public List<UserBetSupport> listUserBetSupport(Long userId, UserBetSupportStatus[] status) {
        return userBetDao.listUserBetSupport(userId, status);
    }

    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#loadUserBet(java.lang.Long, com.fitbet.entity.bet.UserBetStatus[])
     */
    public UserBet loadUserBet(Long userBetId, UserBetStatus[] status) {
        return userBetDao.loadUserBet(userBetId, status);
    }

    /*
     * (non-Javadoc)
     * @see com.fitbet.api.service.UserBetService#loadUserBet(java.lang.Long, java.lang.Long)
     */
    public UserBet loadUserBet(Long userId, Long betId, UserBetStatus[] status) {
        return userBetDao.loadUserBet(userId, betId, status);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.api.service.UserBetService#listSupports(com.fitbet.entity.bet.UserBet)
     */
    @SuppressWarnings("unchecked")
    public List<UserBetSupport> listSupports(UserBet userBet) {
        return genericDaoSupport.searchForList("FROM UserBetSupport WHERE disabled = false AND userBet = :userBet", "userBet", userBet);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#loadSupports(com.fitbet.entity.bet.UserBet, com.fitbet.entity.user.User)
     */
    public UserBetSupport loadSupports(UserBet userBet, User user) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userBet", userBet);
        params.put("user", user);
        return genericDaoSupport.searchForObject("FROM UserBetSupport WHERE disabled = false AND userBet = :userBet AND user = :user", params, UserBetSupport.class);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.api.service.UserBetService#createUserBet(com.fitbet.entity.user.User, com.fitbet.entity.bet.Bet)
     */
    public Map<String, Object> createUserBet(User user, Bet bet) {
        
        Map<String, Object> result = new HashMap<String, Object>();
        
        UserBet userBet = new UserBet(user, bet);
        genericDaoSupport.save(userBet.init());
        
        UserBetSupport userBetSupport = new UserBetSupport(user, userBet, true);
        genericDaoSupport.save(userBetSupport.init());
        
        SalesOrder salesOrder = new SalesOrder(userBet);
        genericDaoSupport.save(salesOrder);
        
        result.put("userBet", userBet);
        result.put("salesOrder", salesOrder);
        
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#openUserBet(java.lang.Long, java.lang.Long, java.lang.Float)
     */
    public UserBet openUserBet(Long userId, Long betId, Float weight, String beforeVedio) {
        UserBet userBet = userBetDao.loadUserBet(userId, betId, new UserBetStatus[]{UserBetStatus.PAID});
        genericDaoSupport.update(userBet.open().updateWeight(weight, "open").updateVideo(beforeVedio, "open", "open"));
        return userBet;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#closeUserBet(java.lang.Long, java.lang.Long)
     */
    public UserBet closeUserBet(Long userId, Long betId, Float weight, String afterVedio) {
        
        UserBet userBet = userBetDao.loadUserBet(userId, betId,  new UserBetStatus[]{UserBetStatus.PROCESSING});
        genericDaoSupport.update(userBet.close().updateWeight(weight, "close").updateVideo(afterVedio, "close", "open"));
        
        return userBet;
    }
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#uploadVideo(java.lang.Long, java.lang.Long, java.lang.String, java.lang.String)
     */
    public UserBet uploadVideo(Long userId, Long betId, String videoPath, String phase, String status) {
        
        UserBet userBet = userBetDao.loadUserBet(userId, betId,  new UserBetStatus[]{UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED, UserBetStatus.PROCESSING, UserBetStatus.FINISHED});
        genericDaoSupport.update(userBet.updateVideo(videoPath, phase, status));
        return userBet;
    }
     
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.service.UserBetService#createUserBetSupport(com.fitbet.entity.user.User, java.lang.Long, boolean)
     */
    public Map<String, Object> createUserBetSupport(User user, Long userBetId, boolean support) {
        
        Map<String, Object> result = new HashMap<String, Object>();
        
        UserBet userBet = userBetDao.loadUserBet(userBetId, new UserBetStatus[]{UserBetStatus.PAID,UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE, UserBetStatus.CLOSEFAILED, UserBetStatus.PROCESSING, UserBetStatus.FINISHED});
        
        UserBetSupport userBetSupport = new UserBetSupport(user, userBet, support);
        genericDaoSupport.save(userBetSupport.init());
        
        SalesOrder salesOrder= new SalesOrder(userBetSupport);
        genericDaoSupport.save(salesOrder);
        
        result.put("userBetSupport", userBetSupport);
        result.put("salesOrder", salesOrder);
        
        return result;
    }

}
